<?php
class links_admin_approval
{
	var $mydirname;
	var $controller_name;
	var $Links;
	var $constprefMI;

	var $nav_html = NULL;
	var $navinfo = NULL;

	/** Constructor
	 *
	**/
	function links_admin_approval($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();

		$this->constprefMI = '_MI_' . $this->mydirname;
	}

	function index($status = 0, $offset = 0)
	{
		$per_page = 20;
		$sql_status = $status * -1;

		$this->Links->setWhere('status', $sql_status, '=');
		$total_count = $this->Links->getCount() ;

		$data = array();
		if ($total_count > 0) {
			$this->Links->setWhere('status', $sql_status, '=');
			$this->Links->setOrder('date DESC');
			$data = $this->Links->getDatas($per_page, $offset);

			if ($total_count > $per_page) {
				$this->_set_navigation($total_count, $per_page, $status, $offset);
			}
		}

		$db =& Database::getInstance();
		foreach ($data as $key => $d) {
			$data[$key]['email'] = '';
			if ($d['submitter'] > 0) {
				$result = $db->query("SELECT email FROM ". $db->prefix('users') ." WHERE uid=". intval($d['submitter']));
				list($email) = $db->fetchRow($result);
				$data[$key]['email'] = htmlspecialchars(links_urlCheckReplace($email), ENT_QUOTES, _CHARSET);
			}
		}

		$data = array(
			'approvals'      => $data,
			'totalapprovals' => $total_count,
			'status'         => $status,
			'mod_name'       => htmlspecialchars($this->Links->module_name, ENT_QUOTES, _CHARSET),
			'menu_name'      => htmlspecialchars(constant($this->constprefMI . '_ADMENU5'), ENT_QUOTES, _CHARSET),
			'gticket'        => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_admin_approval'),
			'nav'            => $this->nav_html,
			'navinfo'        => $this->navinfo,
		);

		$template = $this->mydirname ."_admin_approval.html";
		$this->Links->adminView($template, $data);
	}

	function delete()
	{
		$this->Links->basicPostCheck('links_admin_approval', FALSE);
		$delete_lid = ($_POST['delete_lid']) ? intval($_POST['delete_lid']): 0;

		if ($link = $this->Links->getDataByLid($delete_lid)) {
			if ($this->Links->delete($delete_lid)) {
				$message = _MD_A_LINKS_CAT_DELETED;
			} else {
				$message = _MD_A_LINKS_CAT_DELETED_ERR;
			}
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=approval', 2, $message);
			exit;
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=approval', 2, _MD_CANT_GET_DATA);
			exit;
		}
	}

	function yes()
	{
		$approval_lid = isset($_POST['approval_lid']) ? intval($_POST['approval_lid']): 0;
		
		if ($link = $this->Links->getDataByLid($approval_lid)) {
			$status = $link['status'];
			if ($link['status'] == 0 || $link['status'] == -3) {
				$status = 1;
			} else if ($link['status'] == -1) {
				$status = 2;
			}

			if ($this->Links->saveApproval($approval_lid, $status)) {
				$message = _MD_APPROVAL_YES;
			} else {
				$message = _MD_APPROVAL_NO;
			}
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=approval&s='. abs($link['status']), 2, $message);
			exit;
		} else {
			$this->Links->linksRedirectHeader($this->Links->myurl .'/admin/index.php?c=approval', 2, _MD_CANT_GET_DATA);
			exit;
		}
	}

	function _set_navigation($numrows, $per_page, $status = 0, $offset = 0)
	{
		$extra_arg = array(
				's' => $status,
				'p' => $offset,
				'c' => str_replace('admin_', '', $this->controller_name),
				'm' => 'index'
			);
		$options = $this->Links->uri_option_arr;
		$options['url_approach'] = 0;
		$options['admin'] = TRUE;

		$nav = new links_PageNav($numrows, $per_page, $extra_arg, $options, $this->mydirname);
		$this->nav_html = $nav->renderNav(5);

		$last = $offset + $per_page ;
		if( $last > $numrows ) $last = $numrows ;
		$this->navinfo = sprintf( _MD_LINKS_NAVINFO , $offset + 1 , $last , $numrows ) ;
	}

}
